package visual;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JPanel;

import main.GameDef;
import main.GameMain;
import net.Message;

public class Board extends JPanel implements MouseListener, GameDef{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5185500917475988032L;
	public Rectangle castleArea = new Rectangle();
	public int id;
	public String name;
	public Vector<Piece> castle = new Vector<Piece>(0);
	
	public Board(){
		addMouseListener(this);
	}
	
	public void paint(Graphics g){
		g.setColor(new Color(0x000000));
		g.fillRect(0, 0, BOARD_WIDTH, BOARD_HEIGHT);
		if(castleArea != null){
			g.setColor(new Color(0xFF00FF));
			g.fillRect(castleArea.x, castleArea.y, castleArea.width, castleArea.height);
			for(int i = 0; i < castle.size(); i++){
				castle.get(i).paint(g);
			}
		}
		g.setColor(new Color(0xFFFFFF));
		for(int i = 0; i < BOARD_PIECES_Y; i++){
			g.drawLine(0, i * BLOCK_HEIGHT, BOARD_WIDTH, i * BLOCK_HEIGHT);
		}
		for(int i = 0; i < BOARD_PIECES_X; i++){
			g.drawLine(i * BLOCK_WIDTH, 0, i * BLOCK_WIDTH, BOARD_HEIGHT);
		}
	}
	
	public Dimension getPreferredSize(){
		return new Dimension(BLOCK_WIDTH * BOARD_WIDTH, BLOCK_WIDTH * BOARD_WIDTH);
	}
	
	public Dimension getMaximumSize(){
		return getPreferredSize();
	}
	
	public Dimension getMinimumSize(){
		return getPreferredSize();
	}
	
	public void mouseClicked(MouseEvent e) {
		if(e.getButton() == MouseEvent.BUTTON1){
			if(FrameHandler.hasToPutCastle){
				if(setCastlePosition(e.getPoint())){
					FrameHandler.hasToPutCastle = false;
					paint(getGraphics());
					Vector position = new Vector(0);
					position.add(castleArea.getLocation());
					GameMain.client.sendMessage(new Message("castle position", position, false));
				}
			}else if(FrameHandler.hasToPutPiece){
				boolean vertical = FrameHandler.hasToPutPieceVertical;
				Piece tmpPiece = new Piece(FrameHandler.hasToPutPieceId, e.getPoint(), vertical);
				Vector piece = new Vector(0);
				piece.add(tmpPiece);
				GameMain.client.sendMessage(new Message("piece position", piece, false));
			}else if(FrameHandler.playerCanAttack){
				Vector position = new Vector(0);
				position.add(e.getPoint());
				GameMain.client.sendMessage(new Message("attack position", position, false));
			}else if(FrameHandler.hasToRepair){
				Vector position = new Vector(0);
				position.add(e.getPoint());
				GameMain.client.sendMessage(new Message("repair position", position, false));
				FrameHandler.hasToRepair = false;
			}
		} else if(e.getButton() == MouseEvent.BUTTON3){
			FrameHandler.hasToPutPieceVertical = !FrameHandler.hasToPutPieceVertical;
		}
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}
	
	public boolean setCastlePosition(Point pos){
		int x = pos.x - (pos.x % BLOCK_WIDTH);
		int y = pos.y - (pos.y % BLOCK_HEIGHT);
		if(x + CASTLE_WIDTH > (BOARD_WIDTH)){
			return false;
		}
		if(y + CASTLE_HEIGHT > (BOARD_HEIGHT)){
			return false;
		}
		castleArea = new Rectangle(x, y, CASTLE_WIDTH, CASTLE_HEIGHT);
		return true;
	}
}
